﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using INTREPWEB.Models;

namespace INTREPWEB.Controllers
{ 
    public class EmbarqueController : Controller
    {
        private INTREPDB db = new INTREPDB();

        //
        // GET: /Boarding/

        public ViewResult Index()
        {
            var model = db.Embarques.OrderBy(x => x.DataInicio).ToList();

            if (model.Count == 0)
                ViewBag.Vazio = true;

            return View(model);
        }

        //
        // GET: /Boarding/Details/5

        public ViewResult Ver(int id)
        {
            Embarque e = db.Embarques.Find(id);
            return View(e);
        }

        //
        // GET: /Boarding/Create

        public ActionResult Criar()
        {
            EmbarqueViewModel model = new EmbarqueViewModel();

            return View(model);
        } 

        //
        // POST: /Boarding/Create

        [HttpPost]
        public ActionResult Criar(EmbarqueViewModel boardingvm)
        {
            if (ModelState.IsValid)
            {
                Embarque b = new Embarque();
                var startdate = boardingvm.DataInicio.AddHours(boardingvm.HoraInicio.Hour).AddMinutes(boardingvm.HoraInicio.Minute);
                b.Id = startdate.ToString("yyyyMMddHHmmss");
                b.DataInicio = startdate;
                b.DataFim = boardingvm.DataFim.AddHours(boardingvm.HoraFim.Hour).AddMinutes(boardingvm.HoraFim.Minute);
                b.Navio = db.Navios.Find(boardingvm.NavioId);

                db.Embarques.Add(b);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(boardingvm);
        }
        
        //
        // GET: /Boarding/Edit/5

        public ActionResult Editar(String id)
        {
            Embarque boarding = db.Embarques.Find(id);

            EmbarqueViewModel model = new EmbarqueViewModel(id);
            model.Id = boarding.Id;
            model.DataInicio = boarding.DataInicio;
            model.DataFim = boarding.DataFim;
            model.NavioId = boarding.Navio.Id;

            return View(model);
        }

        //
        // POST: /Boarding/Edit/5

        [HttpPost]
        public ActionResult Editar(EmbarqueViewModel boardingvm)
        {
            if (ModelState.IsValid)
            {
                Embarque b = db.Embarques.Find(boardingvm.Id);

                var startdate = boardingvm.DataInicio.AddHours(boardingvm.HoraInicio.Hour).AddMinutes(boardingvm.HoraInicio.Minute);
                b.DataInicio = startdate;
                b.DataFim = boardingvm.DataFim.AddHours(boardingvm.HoraFim.Hour).AddMinutes(boardingvm.HoraFim.Minute);
                b.Navio = db.Navios.Find(boardingvm.NavioId);
                
                db.Entry(b).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(boardingvm);
        }

        //
        // GET: /Boarding/Delete/5

        public ActionResult Deletar(String id)
        {
            Embarque boarding = db.Embarques.Find(id);
            return View(boarding);
        }

        //
        // POST: /Boarding/Delete/5

        [HttpPost, ActionName("Deletar")]
        public ActionResult DeleteConfirmed(String id)
        {            
            Embarque boarding = db.Embarques.Find(id);
            db.Embarques.Remove(boarding);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}